<!--
  -    Copyright (c) 2018-2025, hycloud All rights reserved.
  -
  - Redistribution and use in source and binary forms, with or without
  - modification, are permitted provided that the following conditions are met:
  -
  - Redistributions of source code must retain the above copyright notice,
  - this list of conditions and the following disclaimer.
  - Redistributions in binary form must reproduce the above copyright
  - notice, this list of conditions and the following disclaimer in the
  - documentation and/or other materials provided with the distribution.
  - Neither the name of the pig4cloud.com developer nor the names of its
  - contributors may be used to endorse or promote products derived from
  - this software without specific prior written permission.
  - Author: hycloud
  -->

<template>
  <div class="execution">
    <basic-container>
      <el-row :gutter="20">
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-select v-model="q.dsName" placeholder="请选择数据源" @change="search">
              <el-option
                v-for="item in dataSourceList"
                :key="item.id"
                :label="item.name"
                :value="item.name"/>
            </el-select>
          </div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple">
            <el-input v-model="q.tableName" placeholder="表名称"/>
          </div>
        </el-col>
        <el-col :span="12">
          <div class="grid-content bg-purple">
            <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button type="primary" icon="el-icon-download" @click="openBatch">批量生成</el-button>
          </div>
        </el-col>
      </el-row>
      <avue-crud
        ref="crud"
        :page.sync="page"
        :data="tableData"
        :table-loading="tableLoading"
        :option="tableOption"
        @on-load="getList"
        @size-change="sizeChange"
        @current-change="currentChange"
        @refresh-change="refreshChange">
        <template
          slot-scope="scope"
          slot="menu">
          <el-button
            type="text"
            icon="el-icon-view"
            @click="handleTable(scope.row,scope.index)">字段
          </el-button>
          <el-button
            type="text"
            icon="el-icon-check"
            @click="handleDown(scope.row,scope.index)">生成
          </el-button>
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleDesign(scope.row,scope.index)">设计
          </el-button>
        </template>
      </avue-crud>

      <el-dialog
        :visible.sync="box"
        title="生成配置"
        width="50%"
        lock-scroll>
        <div class="pull-auto">
          <avue-form
            ref="formData"
            :option="formOption"
            v-model="formData">
            <template slot-scope="scope" slot="menuForm">
              <el-button type="primary"
                         icon="el-icon-view"
                         plain
                         @click="handleView()"
              >预览
              </el-button>
              <el-button type="info"
                         icon="el-icon-check"
                         plain
                         @click="gen()"
              >下载
              </el-button>
            </template>
          </avue-form>
        </div>
      </el-dialog>
      <el-dialog
        :visible.sync="boxBatch"
        title="批量生成"
        width="50%"
        lock-scroll>
        <div class="pull-auto">
          <avue-form
            ref="formBatchData"
            :option="formBatchOption"
            v-model="formBatchData"
            @submit="batchGen"/>
        </div>
      </el-dialog>

    </basic-container>
    <!-- 预览界面 -->
    <el-dialog :title="preview.title" :visible.sync="preview.open" width="80%" top="5vh" append-to-body>
      <Preview :queryData="this.formData" v-if="preview.open"/>
    </el-dialog>
    <!--表字段-->
    <el-dialog :title="table.title" :visible.sync="table.open" width="80%" top="5vh" append-to-body>
      <Table :queryData="this.formData" v-if="table.open"/>
    </el-dialog>
     <el-dialog
        :visible.sync="modal"
        :fullscreen="true"
        class="showlabelDia"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        @close="modal = false"
      >
        <table-design
          v-if="modal"
          :showGithubStar="false"
          :toolbar="toolbar"
          @save="templateSave"
          :import-json="editJson"
        ></table-design>
      </el-dialog>
      <el-dialog
        title="模板名称"
        width="400px"
        :visible.sync="modal2"
        :close-on-click-modal="false"
        :destroy-on-close="true"
        class="module-dialog"
        @close="modal2 = false"
      >
        <el-input
          disabled
          v-model="temParams.tableName"
        ></el-input>
          <el-input
          v-model="temParams.formName"
          placeholder="请输入表单名称"
        ></el-input>
        <el-select
          v-model="temParams.moduleType"
          placeholder="请选择模块类型"
        >
          <el-option
            v-for="item in dicList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <span slot="footer" class="dialog-footer">
          <el-button @click="modal2 = false">取 消</el-button>
          <el-button type="primary" @click="temSave" :loading="loading"
            >确 定</el-button
          >
        </span>
      </el-dialog>
  </div>
</template>

<script>
  import {addObj, delObj, putObj} from '@/api/gen/form'
  import {fetchList, fetchSelectDsList, handleDown, getForm} from '@/api/gen/gen'
  import {formBatchOption, formOption, tableOption} from '@/const/crud/gen/gen'
  import Preview from './preview'
  import { validatenull } from "@/util/validate";
  import { remote } from "@/api/admin/dict";
  import Table from './table'

  export default {
    name: 'CodeGenerator',
    components: {Preview, Table},
    data() {
      return {
        q: {},
        dataSourceList: [],
        tableData: [],
        formData: {},
        formBatchData: {},
        box: false,
        boxBatch: false,
        page: {
          total: 0, // 总页数
          currentPage: 1, // 当前页数
          pageSize: 20 // 每页显示多少条
        },
        tableLoading: false,
        tableOption: tableOption,
        formOption: formOption,
        formBatchOption: formBatchOption,
        modal: false,
        modal2: false,
        loading: false,
        searchForm: {},
        temParams: {
          id: '',
          moduleType: '',
          formName: '',
          tableName: ''
        },
        dicList: [],
        jsonData: "",
        editJson: "",
        toolbar: ["generate", "preview", "clear"],
        // 预览参数
        preview: {
          open: false,
          title: "代码预览"
        },
        table: {
          open: false,
          title: "字段预览"
        }
      }
    },
    created() {
      this.getdataSourceList()
      remote('module_type').then(response => {
        this.dicList = response.data.data
      })
    },
    methods: {
      getList(page) {
        this.tableLoading = true
        fetchList(Object.assign({
          current: page.currentPage,
          size: page.pageSize
        }, this.q)).then(response => {
          this.tableData = response.data.data.records
          this.page.total = response.data.data.total
          this.tableLoading = false
        })
      },
      templateSave(data) {
        this.modal2 = true;
        this.jsonData = data;
      },
      temSave() {
        this.loading = true;
        if (!this.temParams.tableName) {
          this.$message.error("请输入表名称");
          this.loading = false;
          return;
        }
        if (!this.temParams.formName) {
          this.$message.error("请输入表单名称");
          this.loading = false;
          return;
        }
        if (!this.temParams.moduleType) {
          this.$message.error("请选择模块类型");
          this.loading = false;
          return;
        }
        this.temParams.formInfo = this.jsonData
        if (!this.temParams.id) {
          addObj(this.temParams)
            .then((res) => {
              this.$message.success("新增模板成功");
              this.modal = false;
              this.modal2 = false;
              this.getList(this.page);
            })
            .finally(() => (this.loading = false));
        } else {
          putObj(this.temParams)
            .then((res) => {
              this.$message.success("编辑模板成功");
              this.modal = false;
              this.modal2 = false;
              this.getList(this.page);
            })
            .finally(() => (this.loading = false));
        }
      },
      handleView: function () {
        this.formData.dsName = this.q.dsName
        this.preview.open = true
        this.table.open = false
      },
      handleDesign: function (row) {
        this.temParams.tableName = row.tableName
        this.getFormInfo(row);
        // this.$router.push({path: '/gen/design', query: {tableName: row.tableName, dsName: this.q.dsName}})

      },
       getFormInfo(row) {
        getForm(row.tableName, row.dsName).then((response) => {
          if (!validatenull(response.data.data)) {
            this.editJson = response.data.data;
            this.modal = true;
          }
        });
      },
      handleTable: function (row) {
        this.formData.tableName = row.tableName
        this.formData.dsName = this.q.dsName
        this.table.open = true
        this.preview.open = false
      },
      handleDown: function (row) {
        this.formData.tableName = row.tableName
        this.box = true
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize
      },
      currentChange(current) {
        this.page.currentPage = current
      },
      refreshChange() {
        this.getList(this.page)
      },
      gen() {
        this.formData.dsName = this.q.dsName
        handleDown(this.formData).then(() => {
          this.box = false
        })
      },
      getdataSourceList() {
        fetchSelectDsList().then(response => {
          this.dataSourceList = response.data.data
        })
      },
      search() {
        this.getList(this.page)
      },
      openBatch() {
        if (this.$refs.crud.tableSelect.length <= 1 || this.$refs.crud.tableSelect.length > 10) {
          this.$message.error('选中表数量不合法，数量最少2个或最多为10个')
          return false
        }
        let tableName = []
        for (const table of this.$refs.crud.tableSelect) {
          tableName.push(table.tableName)
        }
        this.formBatchData.tableName = tableName.join('-')
        this.boxBatch = true
      },
      batchGen(form, done) {
        this.formBatchData.dsName = this.q.dsName
        handleDown(this.formBatchData).then(() => {
          done()
          this.boxBatch = false
        }).catch(() => {
          done()
        })
      }
    }
  }
</script>

<style lang="scss">
.module-dialog{
  .el-input {
    margin-bottom: 10px;
  }
  .el-select{
    width: 100%;
  }
}
.showlabelDia{
  .widget-form-list{
    .el-form-item__label{
      display: inline-block;
      margin-left: 10px;
    }
    .el-form-item__content{
      margin-left: 0px !important;
    }
  }
}
</style>

